﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExpenseTracker.Entity;
using ExpenseTracker.Controller;

namespace ExpenseTracker
{
    public partial class CategoryForm : BaseForm
    {
        public delegate void CategoryFormSaved();
        public event CategoryFormSaved OnCategoryFormSaved;

        private ExpenseTracker.Controller.CategoryController _categoryController;
        private ExpenseTracker.Entity.Category _category;

        public CategoryForm(ExpenseTracker.Entity.Account account)
            : base(account)
        {
            _categoryController = new CategoryController(account);
            _category = new Category();
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            PopulateForm();
            base.OnLoad(e);
        }

        public ExpenseTracker.Entity.Category Category 
        {
            get { return _category; }
            set { _category = value; }
        }

        protected void PopulateForm()
        {
            if (FormAction == FormAction.Update && Category.CategoryID > 0)
            {
                Category = _categoryController.GetCategory(Category.CategoryID);
                txtTitle.Text = Category.Title;
            }
        }

        protected bool ValidateForm()
        {
            bool valid = true;
            StringBuilder sb = new StringBuilder();

            if (String.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                sb.Append("- Enter title." + Environment.NewLine);
                valid = false;
            }

            if (!valid)
            {
                MessageBox.Show("Error:" + Environment.NewLine + sb.ToString());
            }

            return valid;
        }

        protected void FormToObject()
        {
            if (FormAction == FormAction.Create)
            {
                Category = new Category();
                Category.TransactionType = TransactionType.Expense;
                Category.Title = txtTitle.Text.Trim();
            }
            else if (FormAction == FormAction.Update)
            {
                Category.Title = txtTitle.Text.Trim();                
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                FormToObject();
                if (FormAction == FormAction.Create)
                {
                    _categoryController.InsertCategory(Category);
                }
                else if (FormAction == FormAction.Update)
                {
                    _categoryController.UpdateCategory(Category);
                }

                if (OnCategoryFormSaved != null)
                {
                    OnCategoryFormSaved();
                }
                this.Close();
            }
        }

        private void txtTitle_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                btnSave_Click(this, null);
            }
        }
    }
}
